package com.zx.account.controller;

import com.google.common.base.Splitter;
import com.zx.account.entity.Month;
import com.zx.account.entity.User;
import com.zx.account.model.MonthAccountDto;
import com.zx.account.model.YearAccountDto;
import com.zx.account.service.ATypeService;
import com.zx.account.service.AccountService;
import com.zx.account.service.MonthService;
import com.zx.util.SessionDataUtil;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;


/**
 * @version v1.0
 * @Project: managing-self
 * @Title: AccountYearController
 * @Description: 消费年统计
 * @author: zhangxue
 * @date: 2019年10月4日下午10:15:35
 * @company: 未知
 * @Copyright: Copyright (c) 2015
 */
@Controller
@RequestMapping("/account")
public class AccountYearController {

    private static final Splitter SPLITTER = Splitter.on(",").trimResults();
    @Autowired
    private AccountService accountService;
    @Autowired
    private ATypeService aTypeService;
    @Autowired
    private MonthService monthService;

    /**
     * 初始化年统计页面
     *
     * @param m
     * @return
     */
    @GetMapping("/yearView")
    public ModelAndView initYearAccount(ModelAndView m) {
        User user = SessionDataUtil.getUserInfo();
        List<Month> month = this.monthService.getByUserId(user.getId());
        List<Integer> years = month.stream()
                .map(Month::getAYear)
                .distinct()
                .sorted()
                .collect(Collectors.toList());
        m.addObject("types", this.aTypeService.getByUserId(user.getId()));
        m.addObject("years", years);
        m.setViewName("account/yearView");
        return m;
    }

    /**
     * 初始化年统计页面
     * @return
     */
    @RequestMapping("/getMonthAccount")
    @ResponseBody
    public Map<String, List> getMonthAccount(Integer year, String ts) {
        User user = SessionDataUtil.getUserInfo();
        List<Integer> tidsTemp = SPLITTER.splitToList(ts).stream().map(Integer::parseInt).collect(Collectors.toList());
        int[] tids = new int[tidsTemp.size()];
        for (int i = 0; i < tidsTemp.size(); i++) {
            tids[i] = tidsTemp.get(i);
        }
        // 当年汇总
        List<MonthAccountDto> currData = this.accountService.getMonthAccount(year, tids, user.getId());
        Map<Integer, MonthAccountDto> currMap = currData.stream()
                .collect(Collectors.toMap(MonthAccountDto::getMonth, a -> a));

        // 上一年汇总
        List<MonthAccountDto> lastData = this.accountService.getMonthAccount(year - 1, tids, user.getId());
        Map<Integer, MonthAccountDto> lastMap = lastData.stream()
                .collect(Collectors.toMap(MonthAccountDto::getMonth, a -> a));

        //x轴
        List<Integer> x = new ArrayList<>();
        for (MonthAccountDto account : lastData) {
            x.add(account.getMonth());
        }

        // 当年数据
        List<Integer> currRes = new ArrayList<>();
        // 上一年数据
        List<Integer> lastRes = new ArrayList<>();
        for (Integer i : x) {
            MonthAccountDto curr = currMap.get(i);
            currRes.add(curr == null ? 0 : curr.getPrice().intValue());

            MonthAccountDto last = lastMap.get(i);
            lastRes.add(last == null ? 0 : last.getPrice().intValue());
        }

        Map<String, List> map = new HashMap<>();
        map.put("curr", currRes);
        map.put("last", lastRes);
        map.put("x", x);
        return map;
    }

    @RequestMapping("/getYearAccount")
    @ResponseBody
    public Map<String, List> getYearAccount(Integer year, String ts) {
        User user = SessionDataUtil.getUserInfo();
        List<Integer> tidsTemp = SPLITTER.splitToList(ts).stream().map(Integer::parseInt).collect(Collectors.toList());
        int[] tids = new int[tidsTemp.size()];
        for (int i = 0; i < tidsTemp.size(); i++) {
            tids[i] = tidsTemp.get(i);
        }

        int beginYear = year - 10;
        int endYear = year;

        List<YearAccountDto> yearAccount = this.accountService.getYearAccount(beginYear, endYear, tids, user.getId());

        List<String> x = new ArrayList<>();//x轴
        List<Integer> result = new ArrayList<>();//数据
        for (YearAccountDto account : yearAccount) {
            x.add(account.getYear() + "年");
            result.add(account.getPrice().intValue());
        }
        Map<String, List> map = new HashMap<>();
        map.put("x", x);
        map.put("result", result);
        return map;
    }

}
